'use strict';
const axios = require('axios')
const cheerio = require("cheerio")
exports.main = async (event, context) => {
	const db = uniCloud.database();
	const mdb = db.collection('movie-list')
	
	let res = await mdb.field({'aHref':true}).get()  //获得仅包含a连接地址的对象
	let data = res.data
	for(let i=0;i<data.length;i++){

			let commentList = []
			await axios.get(data[i].aHref).then(res=>{
			let $ = cheerio.load(res.data)
			let t = $('#link-report span').text()  //内容简介
			//评论
			$('#hot-comments .comment').each((i,e)=>{			
				let c = {
					userName: "",
					dateTime: "",
					comment: "",
					rate: 0
				}
				c.userName = $(e).find('.comment-info a').text()
				let rate = $(e).find('.comment-info .rating').attr('class')
				if(rate != undefined){
				  rate = parseInt(rate.match(/[0-9]/g).join('')) 
				  c.rate = rate
				}
				let dateTime = $(e).find('.comment-info .comment-time ').text()
				dateTime = dateTime.replace(/\n/g,'')
				c.dateTime = dateTime
				c.comment = $(e).find('.short').text()
				commentList.push(c)
			}) 
			mdb.doc(data[i]._id).update({"content":t,"comment":commentList})    //更新数据
		})
	}
};
